﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_SeeInvisible : Buff
    {
        public Spell_SeeInvisible() 
        {
            minutesLeft = 10;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);
            

            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
            lock (lockitem.LockObj)
            {
                Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                List<MapItem> MapItemsToUpdate = new List<MapItem>();
                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    //Get nearby mapitems that have an invisible creature
                    if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase.buffManager.GetBuff("Invisibility") != null)
                            {
                                MapItemsToUpdate.Add(mapitem);
                                break;
                            }
                        }
                    }
                }
                if (TargetCreatureBase is Character && MapItemsToUpdate.Count > 0)
                {
                    Character targetCharacter = (Character)TargetCreatureBase;
                    targetCharacter.AddMapItems(MapItemsToUpdate, false);
                }
            }

            spellTimer.Start();
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            TargetCreatureBase.buffManager.UpdateBuffs();

            List<MapItem> MapItemsToUpdate = new List<MapItem>();
            List<Character> CharactersWithin12 = new List<Character>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                //Get characters within 12 (for sound)
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 12 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 12)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                            CharactersWithin12.Add((Character)creaturebase);
                    }
                }
                //Get nearby mapitems that have an invisible creature
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase.buffManager.GetBuff("Invisibility") != null)
                        {
                            MapItemsToUpdate.Add(mapitem);
                            break;
                        }
                    }
                }
            }

            if (TargetCreatureBase is Character && MapItemsToUpdate.Count > 0)
            {
                Character targetCharacter = (Character)TargetCreatureBase;
                targetCharacter.AddMapItems(MapItemsToUpdate, false);
            }
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);

            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
            lock (lockitem.LockObj)
            {
                Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                List<MapItem> MapItemsToUpdate = new List<MapItem>();
                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    //Get nearby mapitems that have an invisible creature
                    if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase.buffManager.GetBuff("Invisibility") != null)
                            {
                                MapItemsToUpdate.Add(mapitem);
                                break;
                            }
                        }
                    }
                }
                if (TargetCreatureBase is Character && MapItemsToUpdate.Count > 0)
                {
                    Character targetCharacter = (Character)TargetCreatureBase;
                    targetCharacter.AddMapItems(MapItemsToUpdate, false);
                }
            }
        }
    }
}
